import TileBox from '../../lib/TileTool/TileImpl/TileBox';
import TileUtil from './TileUtil';
const Cesium = require('Cesium/Build/Cesium/index.cjs');


class TileUtilWMTS extends TileUtil {
    schemeName = 'WebMercatorTilingScheme';
    private webMercatorTilingScheme: any;

    constructor() {
        super();
        this.webMercatorTilingScheme = new Cesium.WebMercatorTilingScheme();
    }

    getTileXYZ(lon: number, lat: number, zoom: number) {
        const { x, y } = this.webMercatorTilingScheme.positionToTileXY(
            Cesium.Cartographic.fromDegrees(lon, lat),
            zoom
        );
        return { x, y, zoom };
    }

    getRootTiles() {
        return [{ x: 0, y: 0, zoom: 0 }];
    }

    getBranchTile(xTile: number, yTile: number, zoom: number) {
        let list = [];
        for (let i = 0; i < 4; i++) {
            const x = xTile * 2 + (i % 2);
            const y = yTile * 2 + Math.floor(i / 2);
            const z = zoom + 1;
            list.push({ x, y, zoom: z });
        }
        return list;
    }

    tileXYZToRectangle(x: number, y: number, zoom: number) {
        const radiansRectangle = this.webMercatorTilingScheme.tileXYToRectangle(x, y, zoom);

        const degreeRectangle = {
            west: Cesium.Math.toDegrees(radiansRectangle.west),
            south: Cesium.Math.toDegrees(radiansRectangle.south),
            east: Cesium.Math.toDegrees(radiansRectangle.east),
            north: Cesium.Math.toDegrees(radiansRectangle.north)
        };

        const bb = new TileBox();
        bb.ymax = degreeRectangle.north;
        bb.ymin = degreeRectangle.south;
        bb.xmin = degreeRectangle.west;
        bb.xmax = degreeRectangle.east;
        return bb;
    }

    tile2lon() {
        return 0;
    }

    tile2lat() {
        return 0;
    }
}


export default TileUtilWMTS;
